package com.lwg.practice.java8.behaviparam.stream;

import java.util.Arrays;

import static java.util.Comparator.comparing;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @FileName: PuttingIntoPractice.java
 * @Description: All rights Reserved, Designed By Sunyard
 * Copyright: Copyright(C) 2020-2022
 * Company  : Sunyard.hangzhou
 * @author: 李炜光
 * @slogan:
 * @Date: 2022/1/4 0004
 */
public class PuttingIntoPractice {

    public static void main(String[] args) {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        // 找出2011年的所有交易并按交易额排序
        List<Transaction> collect = transactions.stream()
                .filter(transaction -> transaction.getYear() == 2011) //传递一个谓词来选择2011年的交易
                .sorted(comparing(Transaction::getValue))  //按照交易额排序
                .collect(Collectors.toList());//将生成的stream中的所有元素收集到一个List中
        System.out.println(collect);

        // 交易员都在哪些不同的城市工作过
        List<String> collect1 = transactions.stream()
                .map(transaction -> transaction.getTrader().getCity()) // 提取与交易相关的每位交易员的所在城市
                .distinct()
                .collect(Collectors.toList());
        System.out.println("collect1--------------" + collect1);

        // 查找所有来自于剑桥的交易员，并按姓名排序
        List<Trader> Cambridge = transactions.stream()
                .map(Transaction::getTrader) // 从交易中映射所有的交易员
                .filter(trader -> trader.getCity().equals("Cambridge")) // 选择在城市在剑桥的交易员
                .sorted(comparing(Trader::getName))  // 根据姓名排序
                .collect(Collectors.toList());
        System.out.println("Cambridge---------" + Cambridge);

        // 返回所有交易员的姓名字符串，按字母顺序排序
        String reduce = transactions.stream()
                .map(transaction -> transaction.getTrader().getName())
                .distinct()
                .sorted()
                .reduce("", (n1, n2) -> n1 + n2);
        System.out.println("reduce--------" + reduce);


        // 有没有交易员是在米兰工作的
        boolean milan1 = transactions.stream()
                .anyMatch(transaction -> transaction.getTrader().getCity().equals("Milan"));
        System.out.println("milan1----------" + milan1);


        // 打印生活在剑桥的交易员的所有交易额
        transactions.stream()
                .filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getValue)
                .forEach(System.out::println);

        // 所有交易中，最高的交易额是多少
        Optional<Integer> reduce1 = transactions.stream()
                .map(Transaction::getValue)
                .reduce(Integer::max);
        System.out.println("reduce1------" + reduce1);

        // 找到交易额最小的交易
        Optional<Transaction> min = transactions.stream()
                .min(comparing(Transaction::getValue));
        System.out.println("min----------" + min);
    }
}